
var bridge = {
	default: this,
	call: function(b, a, c) {
		var e = "";
		"function" == typeof a && (c = a, a = {});
		a = { data: void 0 === a ? null : a };
		if ("function" == typeof c) {
			var g = "dscb" + window.dscb++;
			window[g] = c;
			a._dscbstub = g
		}
		a = JSON.stringify(a);
		if (window._dsbridge) e = _dsbridge.call(b, a);
		else if (window._dswk || -1 != navigator.userAgent.indexOf("_dsbridge")) e = prompt("_dsbridge=" + b,
			a);
		return JSON.parse(e || "{}").data
	},
	register: function(b, a, c) {
		c = c ? window._dsaf : window._dsf;
		window._dsInit || (window._dsInit = !0, setTimeout(function() { bridge.call("_dsb.dsinit") },
			0));
		"object" == typeof a ? c._obs[b] = a : c[b] = a
	},
	registerAsyn: function(b, a) { this.register(b, a, !0) },
	hasNativeMethod: function(b, a) { return this.call("_dsb.hasNativeMethod", { name: b, type: a || "all" }) },
	disableJavascriptDialogBlock: function(b) {
		this.call("_dsb.disableJavascriptDialogBlock", {
			disable: !1 !==
				b
		})
	}
};
! function() {
	if (!window._dsf) {
		var b = {
				_dsf: { _obs: {} },
				_dsaf: { _obs: {} },
				dscb: 0,
				dsBridge: bridge,
				close: function() { bridge.call("_dsb.closePage") },
				_handleMessageFromNative: function(a) {
					var e = JSON.parse(a.data),
						b = { id: a.callbackId, complete: !0 },
						c = this._dsf[a.method],
						d = this._dsaf[a.method],
						h = function(a, c) {
							b.data = a.apply(c, e);
							bridge.call("_dsb.returnValue", b)
						},
						k = function(a, c) {
							e.push(function(a, c) {
								b.data = a;
								b.complete = !1 !== c;
								bridge.call("_dsb.returnValue", b)
							});
							a.apply(c, e)
						};
					if (c) h(c, this._dsf);
					else if (d) k(d, this._dsaf);
					else if (c = a.method.split("."), !(2 > c.length)) {
						a = c.pop();
						var c = c.join("."),
							d = this._dsf._obs,
							d = d[c] || {},
							f = d[a];
						f && "function" == typeof f ? h(f, d) : (d = this._dsaf._obs, d = d[c] || {}, (f = d[a]) &&
							"function" == typeof f && k(f, d))
					}
				}
			},
			a;
		for (a in b) window[a] = b[a];
		bridge.register("_hasJavascriptMethod", function(a, b) {
			b = a.split(".");
			if (2 > b.length) return !(!_dsf[
				b] && !_dsaf[b]);
			a = b.pop();
			b = b.join(".");
			return (b = _dsf._obs[b] || _dsaf._obs[b]) && !!b[a]
		})
	}
}();

class XhBridge {
	constructor() {
		this.config();
		this.init();
	}
	//配置参数
	config(params) {
		let defaultConfig = {
			isLoop: false, //是否在循环中
			timeout: 40, //超时时间（秒）
			callbackId: 1, //回调函数id
			callbacks: {}, //回调返回的内容
			callHandleTimeout: null, //定时器
			version: "1.0.1",
			canMonitorCardReader: false, //开监听读身份证
			canMonitorScanCode: false, //开启监听扫码
			canMonitorSensor: false, //开启监听人体传感器
			canMonitorSSCardReader:false,//开启监听社保卡读卡
			openLog: false, //打印console
			...params,
		}
		for (let attr in defaultConfig) {
			this[attr] = defaultConfig[attr];
		}
	}
	//发送消息给容器
	callHandle(params) {
		this.isLoop = true;
		this.callbacks[this.callbackId] = params;
		dsBridge.call(params.method, JSON.stringify({
			timeout: params.timeout || this.timeout,
			callbackId: this.callbackId,
			...params,
		}));

		if (this.callHandleTimeout) clearTimeout(this.callHandleTimeout);
		this.callHandleTimeout = setTimeout(() => {
			delete this.callbacks[this.callbackId];
			this.restData();
			params.fail({
				code: "-1",
				msg: "超时"
			});
		}, (params.timeout || this.timeout) * 1000);
	}
	//重置数据
	restData() {
		this.isLoop = false;
		this.callbackId += 1;
		if (this.callHandleTimeout) clearTimeout(this.callHandleTimeout);
		this.openLog && console.log("重置数据")

	}
	//初始化
	init() {
		let arr = ["getUserInfoByFace", "getScanCode","faceVerify", "getAuthNoByCode", "getUserInfoByIdcard", "getSerialNumber",
			"slipPrint", "reportsPrint","querySlipPrint", "queryReportsPrint","getUserInfoBySScard",'ztKeyboardOn','ztKeyboardOff','realName'];
		for (let i = 0; i < arr.length; i++) {
			dsBridge.register(arr[i], (res) => {
				this.restData();
				this.openLog && console.log(`收到硬件发来数据(success):${res}`)
				res = JSON.parse(res)
				let obj = this.callbacks[res.data.callbackId];
				if (obj) {
					delete this.callbacks[res.data.callbackId];
					delete res.data.callbackId;
					if (res.code == 1) {
						obj.success(res.data);
					} else {
						obj.fail(res);
					}
				}
			})
			XhBridge.prototype[arr[i]] = (params) => {
				if (this.isLoop) {
					this.openLog && console.log("上个命令还没执行完")
					return
				}
				this.openLog && console.log(`调用方法：${arr[i]}`)
				this.callHandle({
					method: arr[i],
					...params,
				})
			}
		}
	}
	//取消刷脸
	exitAlipayFace(params) {
		dsBridge.call("exitAlipayFace");
		this.openLog && console.log("取消刷脸")
	}
	//打开灯光
	lampOpen(params) {
		dsBridge.call("lampOpen",params.type);
		this.openLog && console.log("打开灯光成功")
	}
	//关闭灯光
	lampClose(params) {
		dsBridge.call("lampClose",params.type);
		this.openLog && console.log("关闭灯光成功")
	}
	//播放语音
	speak(params) {
		dsBridge.call("speak", params.text);
		this.openLog && console.log("语音播报成功")
	}
	
	//打开监听读身份证
	onMonitorCardReader(callback) {
		this.canMonitorCardReader = true;
		dsBridge.register("onMonitorCardReader", (res) => {
			if (this.canMonitorCardReader) {
				this.openLog && console.log(`收到硬件发来数据(success):${res}`)
				res = JSON.parse(res)
				callback(res);
			}
		})
		this.openLog && console.log("打开监听身份证成功")
	}
	//关闭监听读身份证
	offMonitorCardReader() {
		this.canMonitorCardReader = false;
		this.openLog && console.log(`关闭监听读身份证`)
	}
	//打开监听扫码
	onMonitorScanCode(callback) {
		this.canMonitorScanCode = true;
		dsBridge.register("onMonitorScanCode", (res) => {
			if (this.canMonitorScanCode) {
				this.openLog && console.log(`收到硬件发来数据(success):${res}`)
				res = JSON.parse(res)
				callback(res);
			}
		})
		this.openLog && console.log("打开监听扫码成功")
	}
	//关闭监听扫码
	offMonitorScanCode() {
		this.canMonitorScanCode = false;
		this.openLog && console.log(`关闭监听扫码`)
	}
	//打开监听人体传感器
	onMonitorSensor(callback) {
		this.canMonitorSensor = true;
		dsBridge.register("sensorListener", (res) => {
			if (this.canMonitorSensor) {
				this.openLog && console.log(`收到硬件发来数据(success):${res}`)
				res = JSON.parse(res)
				callback(res);
			}
		})
		this.openLog && console.log("打开监听人体传感器")
	}
	//关闭监听人体传感器
	offMonitorSensor() {
		this.canMonitorSensor = false;
		this.openLog && console.log(`关闭监听人体传感器`)
	}
	//监听金属键盘
	onKeyBoard(callback) {
		dsBridge.register("onKeyBoard", (res) => {
			this.openLog && console.log(`收到硬件发来数据(success):${res}`)
			res = JSON.parse(res)
			callback(res);
		})
		this.openLog && console.log("监听金属键盘成功")
	}
	//打开监听社保卡读卡
	onMonitorSSCardReader(callback) {
		this.canMonitorSSCardReader   = true;
		dsBridge.register("onMonitorSSCardReader", (res) => { 
			if (this.canMonitorSSCardReader) {
				this.openLog && console.log(`收到硬件发来数据(success):${res}`)
				res = JSON.parse(res)
				callback(res);
			}
		})
		this.openLog && console.log("打开监听社保卡读卡")
	}
	//关闭监听社保卡读卡
	offMonitorSSCardReader() {
		this.canMonitorSSCardReader = false;
		this.openLog && console.log(`关闭监听社保卡读卡`)
	}

}
var XH = new XhBridge();
module.exports = XH
